package cn.gqr.service.Impl;

import cn.gqr.entity.BookType;
import cn.gqr.mapper.TypeMapper;
import cn.gqr.service.TypeService;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    TypeMapper typeMapper;

    //全查询
    @Override
    public List<BookType> findAll() {
        return typeMapper.findAll();
    }

    @Override
    public List<BookType> findAll2() {
        return typeMapper.findAll2();
    }

    //id查询
    @Override
    public BookType findById(int id) {
        return typeMapper.findById(id);
    }

    //添加或者修改
    @Override
    @Transactional
    public Map<String,Object> addOrUpdate(BookType book) {
        int n = 0;
        //添加
        if (book.getId() == 0) {
            n = typeMapper.add(book);
        }
        //修改
        if (book.getId() > 0) {
            n = typeMapper.update(book);
        }
        return RespJson.pack(200, n>0?"成功":"失败",typeMapper.findAll2());
    }
}
